package com.gmail.rborovyk.brightness_widget;

import android.app.Activity;
import android.content.ContentResolver;

import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.SeekBar;


public class BrightnessActivity extends Activity {
	//private static final String TAG = "BrightnessActivity";
	
	private Presets mPrefs;
	private ContentResolver  mResolver;
	private int mBrightness;
	private boolean mIsAutoMode;
	private Button mDoneBtn;
	private CheckBox mAutoMode;
	private boolean mHasAutomaticMode = Presets.HAS_AUTO_MODE;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mResolver = getContentResolver();
		mPrefs = new Presets(this);
		
		mBrightness = Settings.System.getInt(mResolver, Settings.System.SCREEN_BRIGHTNESS, 0);
		
		if(mHasAutomaticMode) {
			int mode = Settings.System.getInt(mResolver, 
					Presets.SCREEN_BRIGHTNESS_MODE, 
					Presets.SCREEN_BRIGHTNESS_MODE_MANUAL);
			mIsAutoMode = (Presets.SCREEN_BRIGHTNESS_MODE_AUTOMATIC == mode);
		}else{
			mIsAutoMode = false;
		}
		
		buildUI();
		updateBrightness();
	}
	
	
	
	private View buildUI() {
		int scaledWidth = getResources().getDimensionPixelSize(R.dimen.bightness_dialog_width);
		
		LinearLayout root = new LinearLayout(this);
		root.setOrientation(LinearLayout.VERTICAL);
		root.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		root.setPadding(5, 5, 5, 5);
		setContentView(root);
		
		LayoutParams contentLayout = new LayoutParams(scaledWidth, LayoutParams.WRAP_CONTENT);
		int cnt = mPrefs.getPresetsCount();
		for(int idx = 0; idx<cnt; ++idx) {
			Presets.Preset p = mPrefs.getPreset(idx);
			Button button = new Button(this);
			button.setText(p.name);
			button.setTag(p.value);
			button.setOnClickListener(mListener);
			root.addView(button, contentLayout);
		}
		
		//temporary boost brightness to simplify selection
		if(mPrefs.getRiseBrightness() && mBrightness < 150)
			mBrightness = 200;
		
		//custom settings
		SeekBar seekBar = new SeekBar(this);
		seekBar.setMax(255);  
		seekBar.setKeyProgressIncrement(1); 
		seekBar.setProgress(mBrightness);  
		seekBar.setOnSeekBarChangeListener(mSeekListener);
		root.addView(seekBar, contentLayout);
		
		LinearLayout horizontal = new LinearLayout(this);
		horizontal.setOrientation(LinearLayout.HORIZONTAL);
		horizontal.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
		horizontal.setPadding(0, 5, 0, 0);
		root.addView(horizontal);
		
		
		LinearLayout.LayoutParams hlayout = new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT);
		hlayout.weight = 1;
	
		if(mHasAutomaticMode) {
			mAutoMode = new CheckBox(this);
			mAutoMode.setText(R.string.brightness_auto);
			mAutoMode.setChecked(mIsAutoMode);
			mAutoMode.setLayoutParams(hlayout);
			horizontal.addView(mAutoMode);
		}
		
		mDoneBtn = new Button(this);
		mDoneBtn.setText(R.string.brightness_set);
		mDoneBtn.setOnClickListener(mListener);
		mDoneBtn.setTag(mBrightness);
		mDoneBtn.setLayoutParams(hlayout);
		horizontal.addView(mDoneBtn);
		
		return root;
	}
	
	private View.OnClickListener mListener = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			if(mHasAutomaticMode && v.equals(mDoneBtn) && mAutoMode.isChecked() && !mIsAutoMode){
				Settings.System.putInt(mResolver, 
						Presets.SCREEN_BRIGHTNESS_MODE, 
						Presets.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
			}else{
				if(mHasAutomaticMode && mIsAutoMode) {
					Settings.System.putInt(mResolver, 
							Presets.SCREEN_BRIGHTNESS_MODE, 
							Presets.SCREEN_BRIGHTNESS_MODE_MANUAL);
				}
				
				mBrightness = (int)(Integer)v.getTag();
				updateBrightness();
				Settings.System.putInt(mResolver, 
						Settings.System.SCREEN_BRIGHTNESS, mBrightness);
			}
			finish();
		}
	};
	
	private SeekBar.OnSeekBarChangeListener mSeekListener = new SeekBar.OnSeekBarChangeListener() {
		@Override
		public void onStopTrackingTouch(SeekBar seekBar) {
			updateBrightness();
		}
		
		@Override
		public void onStartTrackingTouch(SeekBar seekBar) {}
		
		@Override
		public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
			mBrightness = progress;
			mDoneBtn.setTag(mBrightness);
			updateBrightness();
		}
	};
	
	private void updateBrightness() {
		WindowManager.LayoutParams lp = getWindow().getAttributes();
		lp.screenBrightness = mBrightness / (float)255;
		getWindow().setAttributes(lp);
	}
}
